package com.example.lotapp.utils;

import java.util.Locale;

import android.text.TextUtils;


public class DataUtils {
	
	public static double parseDouble(String value, double defValue) {
		if (TextUtils.isEmpty(value)) {
			return defValue;
		}
		double ans  = 0;
		try {
			ans = Double.parseDouble(value);
		} catch (NumberFormatException e) {
			ans = defValue;
			e.printStackTrace();
		}
		return ans;
	}
	
	public static float parseFloat(String value, float defValue) {
		if (TextUtils.isEmpty(value)) {
			return defValue;
		}
		float ans = 0;
		try {
			ans = Float.parseFloat(value);
		} catch (NumberFormatException e) {
			ans = defValue;
			e.printStackTrace();
		}
		return ans;
	}
	
	public static long parseLong(String value, long defValue) {
		if (TextUtils.isEmpty(value)) {
			return defValue;
		}
		long ans = 0;
		try {
			ans = Long.parseLong(value);
		} catch (NumberFormatException e) {
			ans = defValue;
			e.printStackTrace();
		}
		return ans;
	}
	
	public static int parseInt(String value, int defValue) {
		if (TextUtils.isEmpty(value)) {
			return defValue;
		}
		int ans = 0;
		try {
			ans = Integer.parseInt(value);
		} catch (NumberFormatException e) {
			ans = defValue;
			e.printStackTrace();
		}
		return ans;
	}
	
	public static byte parseByte(String value, byte defValue) {
		if (TextUtils.isEmpty(value)) {
			return defValue;
		}
		byte ans = 0;
		try {
			ans = Byte.parseByte(value);
		} catch (NumberFormatException e) {
			ans = defValue;
			e.printStackTrace();
		}
		return ans;
	}
	
	public static String moneyDoubleToStr(double value) {
		return String.format(Locale.US, "%.2f", value);
	}
	
	public static String moneyFloatToStr(float value) {
		return String.format(Locale.US, "%.2f", value);
	}
}
